package com.dianrui.fmi.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.activity.SearchCommunityActivity;
import com.dianrui.fmi.community.PublishCommuityActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 社区的fragment
 *
 * @author Administrator
 */
public class CommunityFragment extends BaseFragment {
    private List<Fragment> fragments;
    private List<String> titles;


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.pager)
    ViewPager pager;


    @Override
    public int getContentViewId() {
        return R.layout.frag_community;
    }

    @Override
    public void initView() {

        title.setText(getString(R.string.main_community_txt));
        loadTabLayout();
    }

    private void loadTabLayout() {
        titles = new ArrayList<>();
        titles.add(getString(R.string.community_hot_txt));
        titles.add(getString(R.string.community_myques_txt));
        titles.add(getString(R.string.community_myanswer_txt));
        titles.add(getString(R.string.community_collect_txt));

        fragments = new ArrayList<>();
        fragments.add(new FragmentHotCommunity());
        fragments.add(new FragmentMyQuestionCommunity());
        fragments.add(new FragmentMyAnswerCommunity());
        fragments.add(new FragmentFavoritesCommunity());

        //预加载
        pager.setOffscreenPageLimit(fragments.size());

        //设置适配器

        FragmentManager childFragmentManager = getChildFragmentManager();
        pager.setAdapter(new FragmentPagerAdapter(childFragmentManager) {
            //选中的ITem
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            //返回Item个数
            @Override
            public int getCount() {
                return fragments.size();
            }
            //设置标题

            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });

        tablayout.setupWithViewPager(pager);


    }

    @Override
    public void getData() {
    }



    @OnClick({R.id.right_img, R.id.search_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.right_img:
                JumpActivitys(PublishCommuityActivity.class);
                break;
            case R.id.search_layout:
                JumpActivitys(SearchCommunityActivity.class);
                break;

                default:
                    break;
        }
    }
}
